package com.lifeverse.entity;

import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.LocalDateTime;

/**
 * 决策历史记录实体类
 * 记录决策过程中的状态变化和操作历史
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "decision_histories", indexes = {
    @Index(name = "idx_decision_history_decision", columnList = "decision_id"),
    @Index(name = "idx_decision_history_timestamp", columnList = "timestamp")
})
public class DecisionHistory extends BaseEntity {
    
    /**
     * 关联的决策
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "decision_id", nullable = false)
    private Decision decision;
    
    /**
     * 操作类型
     */
    @Column(name = "action_type", nullable = false, length = 50)
    private String actionType;
    
    /**
     * 操作描述
     */
    @Column(name = "action_description", length = 500)
    private String actionDescription;
    
    /**
     * 操作前状态
     */
    @Column(name = "previous_state", length = 100)
    private String previousState;
    
    /**
     * 操作后状态
     */
    @Column(name = "new_state", length = 100)
    private String newState;
    
    /**
     * 操作时间戳
     */
    @Column(name = "timestamp", nullable = false)
    private LocalDateTime timestamp;
    
    /**
     * 操作者（系统或用户ID）
     */
    @Column(name = "actor", length = 100)
    private String actor;
    
    /**
     * 操作详细数据（JSON格式）
     */
    @Column(name = "details", columnDefinition = "TEXT")
    private String details;
    
    /**
     * 操作结果
     */
    @Column(name = "result", length = 200)
    private String result;
    
    @PrePersist
    protected void onCreate() {
        super.onCreate();
        if (timestamp == null) {
            timestamp = LocalDateTime.now();
        }
    }
    
    /**
     * 创建决策历史记录
     */
    public static DecisionHistory create(Decision decision, String actionType, String description, 
                                       String previousState, String newState, String actor) {
        DecisionHistory history = new DecisionHistory();
        history.setDecision(decision);
        history.setActionType(actionType);
        history.setActionDescription(description);
        history.setPreviousState(previousState);
        history.setNewState(newState);
        history.setActor(actor);
        history.setTimestamp(LocalDateTime.now());
        return history;
    }
}